/* * Copyright 2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sbq.batch.configurations; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.sql.Connection; /** * @author ilya40umov */ @Configuration @EnableTransactionManagement public class DatabaseConfiguration { @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } @Bean @Primary public DataSource dataSource() { return new LazyConnectionDataSourceProxy(dbcpDataSource()); } @Bean(destroyMethod = "close") public DataSource dbcpDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/batch_db"); dataSource.setUsername("root"); dataSource.setPassword("wakeup"); dataSource.setMaxActive(20); dataSource.setMaxIdle(20); dataSource.setMaxWait(10000); dataSource.setInitialSize(5); dataSource.setValidationQuery("SELECT 1"); dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } }